<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/javascript/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/javascript/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/javascript/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/javascript/images/logo.svg" color="#222">

<link rel="stylesheet" href="/javascript/css/main.css">


<link rel="stylesheet" href="/javascript/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/javascript/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="前端性能优化1.减少HTTP请求次数和请求报文(请求头，请求行，请求体)的大小 发送数据格式尽可能使用json, XML格式大于json  使用雪碧图  使用字体图标  图片base64  图片懒加载(滚动条或者其他使用到的情况，才会加载)  音视频取消预加载  js&#x2F;css文件进行合并压缩  合并: 多个js或者css尽可能只导入一个 压缩：对css, js可以使用压缩    2.缓存 DNS缓">
<meta property="og:type" content="article">
<meta property="og:title" content="前端性能优化">
<meta property="og:url" content="http://yoursite.com/2020/11/07/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/index.html">
<meta property="og:site_name" content="javascript">
<meta property="og:description" content="前端性能优化1.减少HTTP请求次数和请求报文(请求头，请求行，请求体)的大小 发送数据格式尽可能使用json, XML格式大于json  使用雪碧图  使用字体图标  图片base64  图片懒加载(滚动条或者其他使用到的情况，才会加载)  音视频取消预加载  js&#x2F;css文件进行合并压缩  合并: 多个js或者css尽可能只导入一个 压缩：对css, js可以使用压缩    2.缓存 DNS缓">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-07T14:24:00.259Z">
<meta property="article:modified_time" content="2020-11-07T15:08:37.564Z">
<meta property="article:author" content="HC">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/11/07/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>前端性能优化 | javascript</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/javascript/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">javascript</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/javascript/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/javascript/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/11/07/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/javascript/images/avatar.gif">
      <meta itemprop="name" content="HC">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="javascript">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          前端性能优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-11-07 22:24:00 / Modified: 23:08:37" itemprop="dateCreated datePublished" datetime="2020-11-07T22:24:00+08:00">2020-11-07</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前端性能优化"><a href="#前端性能优化" class="headerlink" title="前端性能优化"></a>前端性能优化</h1><h2 id="1-减少HTTP请求次数和请求报文-请求头，请求行，请求体-的大小"><a href="#1-减少HTTP请求次数和请求报文-请求头，请求行，请求体-的大小" class="headerlink" title="1.减少HTTP请求次数和请求报文(请求头，请求行，请求体)的大小"></a>1.减少HTTP请求次数和请求报文(请求头，请求行，请求体)的大小</h2><ul>
<li><p>发送数据格式尽可能使用json, XML格式大于json</p>
</li>
<li><p>使用雪碧图</p>
</li>
<li><p>使用字体图标</p>
</li>
<li><p>图片base64</p>
</li>
<li><p>图片懒加载(滚动条或者其他使用到的情况，才会加载)</p>
</li>
<li><p>音视频取消预加载</p>
</li>
<li><p>js/css文件进行合并压缩</p>
<ul>
<li>合并: 多个js或者css尽可能只导入一个</li>
<li>压缩：对css, js可以使用压缩</li>
</ul>
</li>
</ul>
<h2 id="2-缓存"><a href="#2-缓存" class="headerlink" title="2.缓存"></a>2.缓存</h2><ul>
<li>DNS缓存或预处理, 加载时减少去查找</li>
<li>使用协商缓存或强缓存</li>
<li>建立长连接; connection: keep-alive</li>
<li>设置本地离线存储</li>
</ul>
<h2 id="3-代码优化"><a href="#3-代码优化" class="headerlink" title="3.代码优化"></a>3.代码优化</h2><ul>
<li><p>实现动画: </p>
<ul>
<li>css优先级高于js</li>
<li>requestAnimationFrame 高于定时器</li>
</ul>
</li>
<li><p>减少对dom的操作</p>
</li>
<li><p>函数防抖和节流</p>
</li>
<li><p>尽量使用事件委托</p>
</li>
<li><p>减少对闭包的使用</p>
</li>
<li><p>避免死循环或相互引用</p>
</li>
<li><p>尽可能使用异步编程，避免主线程阻塞</p>
</li>
<li><p>减少使用eval, with</p>
</li>
<li><p>避免使用iframe (渲染速度会变慢)</p>
</li>
<li><p>低耦合高内聚(提高代码的使用率，尽可能的封装函数或组件，不同的功能封装成不同的函数)</p>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/javascript/2020/11/07/%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B8%B2%E6%9F%93%E9%A1%B5%E9%9D%A2%E7%9A%84%E6%AD%A5%E9%AA%A4/" rel="prev" title="浏览器渲染页面的步骤">
      <i class="fa fa-chevron-left"></i> 浏览器渲染页面的步骤
    </a></div>
      <div class="post-nav-item">
    <a href="/javascript/2020/11/16/2020-11-16%E6%80%BB%E7%BB%93/" rel="next" title="2020-11-16总结">
      2020-11-16总结 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前端性能优化"><span class="nav-number">1.</span> <span class="nav-text">前端性能优化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-减少HTTP请求次数和请求报文-请求头，请求行，请求体-的大小"><span class="nav-number">1.1.</span> <span class="nav-text">1.减少HTTP请求次数和请求报文(请求头，请求行，请求体)的大小</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-缓存"><span class="nav-number">1.2.</span> <span class="nav-text">2.缓存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-代码优化"><span class="nav-number">1.3.</span> <span class="nav-text">3.代码优化</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">HC</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/javascript/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HC</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/javascript/lib/anime.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.ui.min.js"></script>

<script src="/javascript/js/utils.js"></script>

<script src="/javascript/js/motion.js"></script>


<script src="/javascript/js/schemes/muse.js"></script>


<script src="/javascript/js/next-boot.js"></script>




  















  

  

</body>
</html>
